import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/homeworks.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/homeworkRecordsDataGrid.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerStudent.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/sidebars/studentSidebar.dart';

class MyHomeworkAssignments extends StatefulWidget {
  static const String routeName = '/student/homework';

  @override
  State<MyHomeworkAssignments> createState() => _MyHomeworkAssignmentsState();
}

class _MyHomeworkAssignmentsState extends State<MyHomeworkAssignments> {
  final box = Hive.box('appconfig');
  final HomeworksController controller = Get.find();
  final PageStyles _pageStyles = PageStyles();

  @override
  void initState() {
    bool isStudent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Student;
    String _classId = box.get(CacheManagerKey['currentclass'].toString());

    if (isStudent) {
      // if (_classId != '') {
      //   controller.fetchStudentLiveHomeworks(_classId).then((items) {
      //     if (items.isNotEmpty) {
      //       setState(() {
      //         homeworks = items;
      //       });
      //     }
      //   });
      // }
      Future.delayed(Duration(milliseconds: 100), () {
        if (_classId != '') {
          controller.fetchStudentLiveHomeworks(_classId);
        }
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build student MyAssignments');

    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.studentPageItems
        .indexWhere((page) => page.route == Get.currentRoute);

    return Scaffold(
      appBar: isSmallScreen ? CustomAppBar(title: 'Homework'.tr) : null,
      drawer: isSmallScreen ? StudentAppDrawer() : null,
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        child: Row(
          children: [
            Visibility(
                visible: !isSmallScreen,
                child: StudentSidebarX(
                    selectedIndex: _selectedSidebarItem,
                    isBigScreen: isBigScreen)),
            Expanded(
              child: Container(
                margin: _pageStyles.mainContainerPageEdgeInsets(),
                child: controller.obx(
                  (data) => HomeworkRecordsDataGrid(controller.liveAssignments),
                  onEmpty: HomeworkRecordsDataGrid(controller.liveAssignments),
                  onLoading: Container(),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
